<?php

class SyncLog extends EJsonRecord
{
	/**
	 * The followings are the available columns in table 'tbl_sync_log':
	 * @var integer $id
	 * @var integer $local_id
	 * @var integer $client_device_id
	 * @var string  $method
	 * @var integer $sync_time
	 * @var integer $author_id
	 */

	/**
	 * Returns the static model of the specified AR class.
	 * @return CActiveRecord the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{sync_log}}';
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'author' => array(self::BELONGS_TO, 'YumUser', 'author_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'Id',
			'local_id' => 'LocalId',
			'sync_time' => 'Sync Time',
			'method' => 'Sync Method',
			'client_device_id' => 'Client device Id',
			'author_id' => 'User',
		);
	}

	/**
	 * @return string the URL that shows the detail of the post
	 */
	public function getUrl()
	{
		return Yii::app()->createUrl('sync/view', array(
			'id'=>$this->id,
		));
	}



	/**
	 * This is invoked when a record is populated with data from a find() call.
	 */
	protected function afterFind()
	{
		parent::afterFind();
	}

	/**
	 * This is invoked before the record is saved.
	 * @return boolean whether the record should be saved.
	 */
	protected function beforeSave()
	{
		if(parent::beforeSave())
		{
			if($this->isNewRecord)
			{
				$this->sync_time=time();
				$this->author_id=Yii::app()->user->id;
			}
			else
				$this->sync_time=time();
			return true;
		}
		else
			return false;
	}

	/**
	 * This is invoked after the record is saved.
	 */
	protected function afterSave()
	{
		parent::afterSave();
	}

	/**
	 * This is invoked after the record is deleted.
	 */
	protected function afterDelete()
	{
		parent::afterDelete();
	}

	/**
	 * Retrieves the list of posts based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the needed posts.
	 */
	public function search($isadmin)
	{
		$criteria=new CDbCriteria;
		$criteria->compare('id',$this->id,true);
		$criteria->compare('method',$this->method);
		$criteria->compare('client_device_id',$this->client_device_id);
		if($isadmin==false){
			$criteria->compare('author_id',Yii::app()->user->id);
		}
		return new CActiveDataProvider('SyncLog', array(
			'criteria'=>$criteria,
			'sort'=>array(
				'defaultOrder'=>'sync_time DESC',
			),
		));
	}
	public static function getLastestSync()
	{
		$criteria=new CDbCriteria;
		$criteria->order = 'id DESC';
		$syncs= array();
		$syncs= SyncLog::model()->findAll($criteria);
		if(sizeof($syncs)>0){
			return $syncs[0];
		}
		return null;
	}

}